﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TodayWhoModel;

namespace TodayWho.adm
{
    public partial class appropriates : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["isAdmin"] != null)
                {
                    if (Session["isAdmin"].ToString() == "True")
                    {
                        DataTable dt = new DataTable();
                        dt.Load(UserModel.RetrieveAppropriatesWithCategoryId(1));
                        gridViewUsers.DataSource = dt;
                        gridViewUsers.DataBind();
                    }
                    else
                    {
                        Response.Redirect("default.aspx");
                    }
                }
                else
                {
                    Response.Redirect("default.aspx");
                }
            }
            
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                if (Session["isAdmin"] != null)
                {
                    if (Session["isAdmin"].ToString() == "True")
                    {
                        DataTable dt = new DataTable();
                        dt.Load(UserModel.RetrieveAppropriatesWithCategoryId(Convert.ToInt32(ddListCategory.SelectedValue)));
                        gridViewUsers.DataSource = dt;
                        gridViewUsers.DataBind();
                    }
                    else
                    {
                        Response.Redirect("default.aspx");
                    }
                }
                else
                {
                    Response.Redirect("default.aspx");
                }
            }

        }

        protected void ddListCategory_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            int categoryId = Convert.ToInt32(ddListCategory.SelectedValue);
            DataTable dt = new DataTable();
            dt.Load(UserModel.RetrieveAppropriatesWithCategoryId(categoryId));
            gridViewUsers.DataSource = dt;
            gridViewUsers.DataBind();
        }

        protected void gridViewUsers_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dw = (DataRowView)e.Row.DataItem;
                if (dw != null)
                {
                    HyperLink hyp = (HyperLink)e.Row.FindControl("hypPreview");
                    hyp.NavigateUrl = "preview.aspx?u=" + dw["username"].ToString();
                }
            }
        }

        protected void gridViewUsers_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            int userId = Convert.ToInt32(gridViewUsers.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[0]);
            
            if (e.CommandName.CompareTo("SetDate") == 0)
            {
                UserDisplayModel userDisplay = new UserDisplayModel();
                userDisplay.UserId = userId;
                userDisplay.SetDateAutomatically();
            }
            //Response.Redirect("appropriates.aspx");
            
        }
    }
}